C# GDI 绘制圆形进度条制作自定义控件 using System;using System.Windows.Forms;using System.ComponentModel;using System.Drawing;using System.Drawing.Drawing2D;namespace ProgressCircleControl{ /// <summary> /// 圆形进度条 /// </summary> public class ProgressCircle : Control { #region variables Timer timer; Graphics canvasGraphic; Image canvas; Image icon; Rectangle rect; int blinkCount = 2; int blinkSpeed = 10; Pen spokePen = Pens.White; int spokeCount = 4; Brush pieBrush = Brushes.LawnGreen; int value = 0; PointF center; #endregion #region properties [DefaultValue(2), Category("Appearance"), Description("Blink count. 0 for disable.")] public int BlinkCount { get { return blinkCount; } set { if (value > -1) { blinkCount = value; Refresh(); } } } [DefaultValue(10), Category("Appearance"), Description("Blink speed.")] public int BlinkSpeed { get { return blinkSpeed; } set { if (value > 0 && value < 256) blinkSpeed = value; } } [DefaultValue(4), Category("Appearance"), Description("Spoke count. Maximum value is 10. 0 for disable.")] public int SpokeCount { get { return spokeCount; } set { spokeCount = value; Invalidate(); } } [DefaultValue(typeof(Color), "White"), Category("Appearance"), Description("Color of spokes.")] public Color SpokeColor { get { return spokePen.Color; } set { spokePen.Color = value; Invalidate(); } } [Category("Appearance"), Description("Icon to display. Maximum size is half of the control.")] public Image Image { get { return icon; } set { icon = value; Invalidate(); } } [DefaultValue(0), Category("Behavior"), Description("Value of percentage.")] public int Value { get { return value; } set { if (value < 0) value = 0; if (value > 100) value = 100; this.value = value; Invalidate(); if (value == 100 && BlinkCount != 0) timer.Start(); else if (timer.Enabled) timer.Stop(); } } private Color valueTextColor = Color.OrangeRed; [Category("Behavior"), Description("进度值的文字颜色,默认是橙红色.")] public Color ValueTextColor { get { return valueTextColor; } set { valueTextColor = value; } } #endregion #region initiators public ProgressCircle() { DoubleBuffered = true; Width = 64; ForeColor = Color.FromArgb(146, 223, 99); spokePen = new Pen(BackColor, 1); pieBrush = new SolidBrush(ForeColor); BackColor = Color.White; ForeColor = Color.LawnGreen; BlinkCount = 2; SpokeColor = Color.White; SpokeCount = 4; center = new PointF(); timer = new Timer(); timer.Interval = 10; timer.Tick = new EventHandler(timer_Tick); OnResize(null); } #endregion #region callbacks int blink = 0; int count = 255; void timer_Tick(object sender, EventArgs e) { if (!timer.Enabled) return; if (blink == BlinkCount) { blink = 0; count = 255; timer.Stop(); Refresh(); } else { count -= 5; if (count == 0) { count = 255; blink ; } else { Refresh(); } } } #endregion #region painters void drawIcon(Graphics g, Image img) { if (img == null) return; g.DrawImageUnscaled(img, (this.ClientSize.Width - img.Width) / 2, (this.ClientSize.Height - img.Height) / 2); } void drawSpokes(Graphics g, int count) { if (count < 1) return; if (count > 10) count = 10; float angleStep = 360f / (count * 2); float angle = 0f; for (int i = 0; i < count; i ) { PointF p1 = angleToPoint(angle, rect.Width / 2); PointF p2 = angleToPoint(angle 180f, rect.Width / 2); g.DrawLine(spokePen, p1, p2); angle = angleStep; } } void drawProgress(Graphics g, float percent) { if (percent < 0 || percent > 100) return; g.FillPie( pieBrush, rect, -90, (float)(360f * Value / 100f) ); } void drawBackground(Graphics g) { g.Clear(Color.Transparent); ((SolidBrush)pieBrush).Color = BackColor; g.FillEllipse(pieBrush, rect); ((SolidBrush)pieBrush).Color = this.ForeColor; } #endregion #region helpers PointF angleToPoint(float angle, float radius) { return new PointF( center.X radius * (float)Math.Cos(angle * Math.PI / 180), center.Y radius * (float)Math.Sin(angle * Math.PI / 180) ); } #endregion #region events protected override void OnBackColorChanged(EventArgs e) { base.OnBackColorChanged(e); } protected override void OnForeColorChanged(EventArgs e) { base.OnForeColorChanged(e); ((SolidBrush)pieBrush).Color = ForeColor; } protected override void OnResize(EventArgs e) { this.Height = this.Width; rect = ClientRectangle; rect.Width -= 1; rect.Height -= 1; center = new PointF(rect.Width / 2, rect.Height / 2); if (!this.ClientSize.IsEmpty) { if (canvas != null) canvas.Dispose(); canvas = new Bitmap(rect.Width, rect.Height); if (canvasGraphic != null) canvasGraphic.Dispose(); canvasGraphic = Graphics.FromImage(canvas); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.Clear(Parent.BackColor); canvasGraphic.SmoothingMode = SmoothingMode.HighQuality; drawBackground(canvasGraphic); if (!timer.Enabled || blink == BlinkCount) drawProgress(canvasGraphic, value); else { ((SolidBrush)pieBrush).Color = Color.FromArgb(count, ForeColor.R, ForeColor.G, ForeColor.B); canvasGraphic.FillEllipse(pieBrush, rect); ((SolidBrush)pieBrush).Color = ForeColor; } drawSpokes(canvasGraphic, SpokeCount); if (icon != null) drawIcon(canvasGraphic, icon); e.Graphics.DrawImageUnscaled(canvas, (ClientSize.Width - canvas.Width) / 2, (ClientSize.Height - canvas.Height) / 2); if (value != 0) { var rect = ClientRectangle; rect.Inflate(0 - Width / 2, 0 - Height / 2); StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; sf.Alignment = StringAlignment.Center; e.Graphics.DrawString(value.ToString() "%", new Font("微软雅黑", 18F, FontStyle.Bold), new SolidBrush(ValueTextColor), rect, sf); } } #endregion }}
下载C# GDI+绘制圆形进度条制作自定义控件用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论